import { createRequire } from "module";
const require = createRequire(import.meta.url);

const express = require("express");

const bodyParser = require("body-parser");

const app = express();

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: true }));

app.listen(3000, () => console.log("server on http://localhost:3000"));

//定义方法
app.get("/getZsgcMsg", function (req, res) {
  res.json([
    {
      word: "问心无愧",
      name: "张三",
      id: "1",
    },
    {
      word: "扪心自问",
      name: "李四",
      id: "2",
    },
    {
      word: "自言自语",
      name: "王小虎",
      id: "3",
    },
    {
      word: "牛气冲天",
      name: "王三",
      id: "4",
    },
    {
      word: "卧虎藏龙",
      name: "高小",
      id: "5",
    },
    {
      word: "笑里藏刀",
      name: "高小三",
      id: "6",
    },
    {
      word: "坐井观天",
      name: "啦三啦",
      id: "7",
    },
  ]);
});
// 搜索
app.post("/search", function (req, res) {
  console.log(req.body);
  res.json(req.body);
});
// 添加
app.post("/add", function (req, res) {
  console.log("req.body", req.body);
  res.json(req.body);
});
// 删除
app.delete("/delete/:id", function (req, res) {
  console.log("req.params.id", req.params.id);
});
//删除接收参数为req.params.id，形式与其他不同，使用时用/拼接res.json(req.params.id) }); // 修改 app.put('/update', function (req, res) { console.log('req.body', req.body) res.json(req.body) });

const puppeteer = require("puppeteer");
app.post("/export/pdf", async function (req, res) {
  const htmlString = `
<html>
  <head>
    <title>Test PDF</title>
  </head>
  <body>
    <h1>Hello, World!</h1>
    <p>This is a test PDF generated from an HTML string.</p>
  </body>
</html>
`;

  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setContent(htmlString);
  const pdf = await page.pdf({ format: "A4" });

  await browser.close();

  res.contentType("application/pdf");
  res.send(pdf);
  res.json(req.body);
});
